package com.qdxwx.web.interceptor;


import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.company.CompanyService;
import com.qdxwx.data.service.user.UserInviteService;
import com.qdxwx.data.service.website.WebsiteNavigateService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.models.company.Company;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 网站配置管理拦截器
 * @author developer
 */
public class WebsiteInterceptor implements HandlerInterceptor {
    //logger
    private final Logger logger = LoggerFactory.getLogger(WebsiteInterceptor.class);
    @Autowired
    private WebsiteProfileService websiteProfileService;
    @Autowired
    private WebsiteNavigateService websiteNavigateService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserInviteService userInviteService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            Company company = companyService.get(1);//.getByDomain(request);
            if (company == null) {
                logger.error("获取机构信息失败！");
                return false;
            }
            //request.getAttribute("siteCompany");
            //网站配置
            request.setAttribute("siteCompany", company);
            //网站导航配置
            request.setAttribute("friendLinks", websiteNavigateService.getListByType("friend"));
            request.setAttribute("tabLinks", websiteNavigateService.getListByType("tab"));
            request.setAttribute("footerLinks", websiteNavigateService.getListByType("footer"));
            request.setAttribute("channelMenus", channelService.getMenus(company.getCompanyId()));
            //服务开关
            Map<String, Object> serviceSwitch = websiteProfileService.getMap(WebsiteProfileType.serviceSwitch.toString());
            request.setAttribute("serviceSwitch", serviceSwitch);
            //读取可能存在的邀请码
            userInviteService.setSession(request);
            //如果是微信浏览器，判断是否强制登录
            if (TKRequest.isWxBrowse(request) && LoginUser.getUserId(request) <= 0) {
                //获得微信登录配置
                Map<String, Object> wxConfig = websiteProfileService.getMap(WebsiteProfileType.wxLogin);
                //判断是否微信强制授权
                if ("ON".equals(wxConfig.get("forceLogin"))) {
                    //判断请求路径，排除/app目录
                    if (!request.getRequestURI().contains("/auth/")) {
                        logger.info("微信强制登录：" + request.getRequestURI());
                        response.sendRedirect("/auth/wechat/login");//跳转微信登录页面
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            logger.error("网站配置出错", e);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
